<template>
	<view class="container"><yxylInputs :inputs="inputs" @confirm="confirm"></yxylInputs></view>
</template>

<script>
import yxylInputs from '@/components/yxyl-inputs/yxyl-inputs.vue';
export default {
	components: { yxylInputs },
	data() {
		return {
			id: '',
			inputs: [],
			agencyId: ''
		};
	},
	onLoad(e) {
		this.agencyId = e.agencyId;
		this.id = e.id;
		if (e.id) {
			uni.setNavigationBarTitle({
				title: '修改地址'
			});
			this.$http
				.request({
					url: 'api/Agency/GetAgencyAddressItem',
					data: {
						Id: e.id
					}
				})
				.then(res => {
					// #ifdef H5
					this.$wechat.hideMenu();
					// #endif
					this.inputs.push({
						title: '姓名',
						key: 'contact',
						placeholder: '请输入名字',
						required: true,
						type: 'text',
						errMsg: '请输入姓名',
						default: res.contact
					});
					this.inputs.push({
						title: '电话号码',
						key: 'telephone',
						placeholder: '请输入电话号码',
						required: true,
						type: 'number',
						errMsg: '请输入电话号码',
						default: res.telephone
					});
					this.inputs.push({ title: '地区', keys: ['provinceId', 'cityId', 'areaId'], type: 'address', level: 3, default: [res.provinceId, res.cityId, res.areaId] });
					this.inputs.push({
						title: '详细地址',
						key: 'address',
						placeholder: '请输入详细地址',
						required: true,
						type: 'text',
						errMsg: '请输入详细地址',
						default: res.address || ''
					});
				});
		} else {
			this.inputs.push({
				title: '姓名',
				key: 'contact',
				placeholder: '请输入名字',
				required: true,
				type: 'text',
				errMsg: '请输入姓名'
			});
			this.inputs.push({
				title: '电话号码',
				key: 'telephone',
				placeholder: '请输入电话号码',
				required: true,
				type: 'number',
				errMsg: '请输入电话号码'
			});
			this.inputs.push({ title: '地区', keys: ['provinceId', 'cityId', 'areaId'], type: 'address', level: 3 });
			this.inputs.push({
				title: '详细地址',
				key: 'address',
				placeholder: '请输入详细地址',
				required: true,
				type: 'text',
				errMsg: '请输入详细地址'
			});
		}
	},
	methods: {
		confirm(e) {
			e.agencyId = this.agencyId;

			if (!this.id) {
				this.$http
					.request({
						url: 'api/Agency/CreateAgencyAddress',
						method: 'PUT',
						data: e
					})
					.then(() => {
						uni.navigateBack();
					});
			} else {
				e.id = this.id;
				this.$http
					.request({
						url: 'api/Agency/UpdateAgencyAddress',
						method: 'POST',
						data: e
					})
					.then(() => {
						uni.navigateBack();
					});
			}
		}
	}
};
</script>

<style lang="scss" scoped></style>
